<?php

namespace app\admin\controller;

use app\admin\model\Notice as ModelNotice;
use app\admin\model\Page;

/**
 * 通知管理的所有操作
 *
 * 时间 ： 2020年4月23日13:06:58
 * 作者 ： 夏立军
 */
class Notice extends AdminBase
{

    // 显示首页数据
    public function index()
    {
        $notice = new ModelNotice();

        // 通知资讯的数量
        $notice_all_count = $notice->notice_all_count();

        $page = new Page();
        $size = config('ams_config.notice_page_size');
        $page_res = $page->getPageRes($notice_all_count, $size, url('Notice/index'));

        // 以分页的数据
        $notice_list = $notice->notice_list($page_res['offset'], $page_res['size']);

        return view('index', [
            'notice_list' => $notice_list,
            'page_res' => $page_res
        ]);
    }

    // 新增通知页面
    public function add()
    {
        return view('add', []);
    }

    // 新增通知
    public function add_notice()
    {
        $image_files = request()->file('image_files');
        if (!$image_files) {
            $image_files = "[]";
        } else {
            $savename = [];
            foreach ($image_files as $file) {
                $savename[] = \think\facade\Filesystem::disk('public')->putFile('notice', $file);
            }
            $image_files = json_encode($savename);
        }

        $title = input('post.title/s', null);
        $content = input('post.content/s', null);
        $author = input('post.author/s', null);
        $is_top = input('post.is_top/d', null);
        $display = input('post.display/d', null);
        $information_source = input('post.information_source/s', null);
        $release_date = input('post.release_date/s', '1970-01-01');

        $release_date = strtotime(date($release_date));

        $aid = session('admin_login')->id;
        $notice = new ModelNotice();
        $add_res = $notice->add_notice($title, $content, $author, $information_source, $release_date, $is_top, $display, $aid, $image_files);

        if (!$add_res) {
            return json(['code' => 1, 'msg' => '新增失败，标题重复']);
        }
        return json(['code' => 0, 'msg' => '新增成功']);
    }

    // 删除图片
    public function del_image()
    {
        $id = input('post.id/d', null);
        $image_url = input('post.image_url/s', null);

        $notice = new ModelNotice();
        $res = $notice->get_notice_data_by_id($id);

        if (!$res) {
            return json(['code' => 1, 'msg' => '该数据不存在']);
        }
        // 解析文件名称
        $image_files = json_decode($res['image_files'], true);
        // 过滤文件名称
        $data = [];
        foreach ($image_files as $row) {
            if ($row == $image_url) {
                try {
                    unlink(app()->getRootPath() . 'public/storage/' . $row);
                } catch (\Throwable $th) {
                }
                continue;
            }
            $data[] = $row;
        }
        $data = json_encode($data);

        // 修改数据库文件
        $update_res = $notice->update_notice_data_by_id_and_data($id, ['image_files' => $data]);
        if (!$update_res) {
            return json(['code' => 2, 'msg' => '删除失败']);
        }
        return json(['code' => 0, 'msg' => '删除成功']);
    }

    // 编辑通知
    public function edit()
    {
        $nid = input('get.nid/d', null);
        if (is_null($nid)) {
            return redirect_index('资讯ID丢失', url('Notice/index'));
        }
        $notice = new ModelNotice();
        $notice_list = $notice->get_notice_data_by_id($nid);

        return view('edit', [
            'notice_list' => $notice_list,
        ]);
    }

    // 保存通知
    public function edit_notice()
    {
        $image_files = request()->file('image_files');
        if (!$image_files) {
            $image_files = "[]";
        } else {
            $savename = [];
            foreach ($image_files as $file) {
                $savename[] = \think\facade\Filesystem::disk('public')->putFile('notice', $file);
            }
            $image_files = json_encode($savename);
        }

        $id = input('post.id/d', null);
        $title = input('post.title/s', null);
        $content = input('post.content/s', null);
        $author = input('post.author/s', null);
        $is_top = input('post.is_top/d', null);
        $display = input('post.display/d', null);
        $information_source = input('post.information_source/s', null);

        $release_date = input('post.release_date/s', '1970-01-01');
        $release_date = strtotime(date($release_date));

        $aid = session('admin_login')->id;

        // 组合数据
        if (is_null($title)) {
            return json(['code' => 1, 'msg' => '标题不能为空']);
        }
        if (is_null($content)) {
            return json(['code' => 2, 'msg' => '内容不能为空']);
        }
        $notice = new ModelNotice();

        $data['title'] = $title;
        $data['content'] = $content;
        $data['author'] = $author;
        $data['is_top'] = $is_top;
        $data['display'] = $display;
        $data['information_source'] = $information_source;
        $data['release_date'] = $release_date;
        $data['aid'] = $aid;

        // 获取数据库中原有的通知图片
        $notice_res = $notice->get_notice_data_by_id($id);
        $notice_res['image_files'] = json_decode($notice_res['image_files']);
        // 获取刚刚上传的图片
        $image_files = json_decode($image_files);
        // 合并两种类型的图片
        $data['image_files'] = json_encode(array_merge($notice_res['image_files'], $image_files));

        // 保存到数据库
        $add_res = $notice->update_notice_data_by_id_and_data($id, $data);

        if (!$add_res) {
            return json(['code' => 3, 'msg' => '保存失败，标题重复']);
        }
        return json(['code' => 0, 'msg' => '保存成功']);
    }

    // 删除通知
    public function del()
    {
        if (request()->post()) {
            $id = input('post.id/d', null);

            $notice = new ModelNotice();
            $notice_del_res = $notice->del_notice_info_by_id($id);

            if (!$notice_del_res) {
                return json(['code' => 1, 'msg' => '删除失败']);
            }

            return json(['code' => 0, 'msg' => '删除成功']);
        }
        return json(['code' => 99, 'msg' => '请求方式错误']);
    }
}
